package com.cbs.module.friend;

import android.content.Context;
import com.cbs.module.friend.entity.*;
import com.cbs.module.friend.friendmodule.*;
import com.cbs.module.friend.sort.Sorter;
import com.cbs.application.activity.CBSActivity;
import com.cbs.applicationutils.Global;
import com.cbs.network.HttpClient;
import com.cbs.runtimecache.RuntimeCache;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by broche on 1/9/16.
 */
public class FriendModule {
    private static FriendModule instance;
    private HttpClient httpClient = Global.getHttpClient().clone();
    private RuntimeCache runtimeCache;

    private FriendModule(Context context) {
        runtimeCache = new RuntimeCache(context, "CBSFriendModule");
    }

    public static void init(Context context) {
        if (instance == null) {
            instance = new FriendModule(context);
        }
    }

    public static void setDebug(boolean debug) {
        instance.httpClient.setDebug(debug);
    }

    public static void friendRequestAdd(CBSActivity context, int friendUserId, ModuleHandler<FriendRequest> handler) {
        instance._friendRequestAdd(context, friendUserId, handler);
    }

    private void _friendRequestAdd(CBSActivity context, int friendUserId, ModuleHandler<FriendRequest> handler) {
        FriendRequestAdd friendRequestAdd = new FriendRequestAdd(context, friendUserId, handler, runtimeCache);
        friendRequestAdd.start();
    }

    public static void friendRequestAgree(CBSActivity context, int friendRequestId, ModuleHandler<FriendRelation> handler) {
        instance._friendRequestAgree(context, friendRequestId, handler);
    }

    private void _friendRequestAgree(CBSActivity context, int friendRequestId, ModuleHandler<FriendRelation> handler) {
        FriendRequestAgree friendRequestAgree = new FriendRequestAgree(context, friendRequestId, handler, runtimeCache);
        friendRequestAgree.start();
    }

    public static void friendRequestReject(CBSActivity context, int friendRequestId, ModuleHandler handler) {
        instance._friendRequestReject(context, friendRequestId, handler);
    }

    private void _friendRequestReject(CBSActivity context, int friendRequestId, ModuleHandler handler) {
        FriendRequestReject friendRequestReject = new FriendRequestReject(context, friendRequestId, handler, runtimeCache);
        friendRequestReject.start();
    }

    public static List<FriendRequest> getFriendRequestListCache() {
        return instance._getFriendRequestListCache();
    }

    private List<FriendRequest> _getFriendRequestListCache() {
        List<FriendRequest> result = (List<FriendRequest>) runtimeCache.getValue(FriendRequestListCache.class);
        return Sorter.sortFriendRequest(result);
    }

    public static void getFriendRequestList(CBSActivity context, ModuleHandler<List<FriendRequest>> handler) {
        instance._getFriendRequestList(context, handler);
    }

    private void _getFriendRequestList(CBSActivity context, ModuleHandler<List<FriendRequest>> handler) {
        FriendRequestList friendRequestList = new FriendRequestList(context, handler, runtimeCache);
        friendRequestList.start();
    }

    public static List<FriendRelation> getFriendListCache() {
        return instance._getFriendListCache();
    }

    private List<FriendRelation> _getFriendListCache() {
        List<FriendRelation> result = (List<FriendRelation>) runtimeCache.getValue(FriendListCache.class);
        return Sorter.sortFriendRelation(result);
    }

    public static List<FriendRelation> getFriendListCache(String key) {
        return instance._getFriendListCache(key);
    }

    private List<FriendRelation> _getFriendListCache(String key) {
        if (key == null || key.equals("")) {
            return _getFriendListCache();
        } else {
            List<FriendRelation> friends = (List<FriendRelation>) runtimeCache.getValue(FriendListCache.class);
            List<FriendRelation> result = new ArrayList<>();
            for (FriendRelation friend : friends) {
                if (friend.getNickname().contains(key) || friend.getNicknamespell().contains(key)) {
                    result.add(friend);
                }
            }
            return Sorter.sortFriendRelation(result);
        }
    }

    public static void getFriendList(CBSActivity context, ModuleHandler<List<FriendRelation>> handler) {
        instance._getFriendList(context, handler);
    }

    private void _getFriendList(CBSActivity context, ModuleHandler<List<FriendRelation>> handler) {
        FriendList friendList = new FriendList(context, handler, runtimeCache);
        friendList.start();
    }

    public static void friendDelete(CBSActivity context, int friendId, ModuleHandler handler) {
        instance._friendDelete(context, friendId, handler);
    }

    private void _friendDelete(CBSActivity context, int friendId, ModuleHandler handler) {
        FriendDelete friendDelete = new FriendDelete(context, friendId, handler, runtimeCache);
        friendDelete.start();
    }

    public static List<FriendRelation> getBlackListCache() {
        return instance._getBlackListCache();
    }

    private List<FriendRelation> _getBlackListCache() {
        List<FriendRelation> result = (List<FriendRelation>) runtimeCache.getValue(BlackListCache.class);
        return Sorter.sortFriendRelation(result);
    }

    public static void getBlackList(CBSActivity context, ModuleHandler<List<FriendRelation>> handler) {
        instance._getBlackList(context, handler);
    }

    private void _getBlackList(CBSActivity context, ModuleHandler<List<FriendRelation>> handler) {
        BlackList blackList = new BlackList(context, handler, runtimeCache);
        blackList.start();
    }

    public static void BlackAdd(CBSActivity context, int toUserId, ModuleHandler<FriendRelation> handler) {
        instance._BlackAdd(context, toUserId, handler);
    }

    private void _BlackAdd(CBSActivity context, int toUserId, ModuleHandler<FriendRelation> handler) {
        BlackAdd blackAdd = new BlackAdd(context, toUserId, handler, runtimeCache);
        blackAdd.start();
    }

    public static void blackDelete(CBSActivity context, int blackId, ModuleHandler handler) {
        instance._blackDelete(context, blackId, handler);
    }

    private void _blackDelete(CBSActivity context, int blackId, ModuleHandler handler) {
        BlackDelete blackDelete = new BlackDelete(context, blackId, handler, runtimeCache);
        blackDelete.start();
    }

    public static boolean isFriend(int userId) {
        return instance._isFriend(userId);
    }

    private boolean _isFriend(int userId) {
        List<FriendRelation> friends = (List<FriendRelation>) runtimeCache.getValue(FriendListCache.class);
        for (int i = 0; i < friends.size(); i++) {
            if (friends.get(i).getRightuserid() == userId) {
                return true;
            }
        }
        return false;
    }

    public static boolean isBlack(int userId) {
        return instance._isBlack(userId);
    }

    private boolean _isBlack(int userId) {
        List<FriendRelation> blacks = (List<FriendRelation>) runtimeCache.getValue(BlackListCache.class);
        for (int i = 0; i < blacks.size(); i++) {
            if (blacks.get(i).getRightuserid() == userId) {
                return true;
            }
        }
        return false;
    }

    public static FriendRelation getFriend(int friendId) {
        return instance._getFriend(friendId);
    }

    private FriendRelation _getFriend(int friendId) {
        List<FriendRelation> friends = (List<FriendRelation>) runtimeCache.getValue(FriendListCache.class);
        for (int i = 0; i < friends.size(); i++) {
            if (friends.get(i).getId() == friendId) {
                return friends.get(i);
            }
        }
        return null;
    }

    public static FriendRelation getFriendByUserId(int userId) {
        return instance._getFriendByUserId(userId);
    }

    private FriendRelation _getFriendByUserId(int userId) {
        List<FriendRelation> friends = (List<FriendRelation>) runtimeCache.getValue(FriendListCache.class);
        for (int i = 0; i < friends.size(); i++) {
            if (friends.get(i).getRightuserid() == userId) {
                return friends.get(i);
            }
        }
        return null;
    }

    public static FriendRelation getBlack(int blackId) {
        return instance._getBlack(blackId);
    }

    private FriendRelation _getBlack(int blackId) {
        List<FriendRelation> blacks = (List<FriendRelation>) runtimeCache.getValue(BlackListCache.class);
        for (int i = 0; i < blacks.size(); i++) {
            if (blacks.get(i).getId() == blackId) {
                return blacks.get(i);
            }
        }
        return null;
    }

    public static FriendRelation getBlackByUserId(int userId) {
        return instance._getBlackByUserId(userId);
    }

    private FriendRelation _getBlackByUserId(int userId) {
        List<FriendRelation> blacks = (List<FriendRelation>) runtimeCache.getValue(BlackListCache.class);
        for (int i = 0; i < blacks.size(); i++) {
            if (blacks.get(i).getRightuserid() == userId) {
                return blacks.get(i);
            }
        }
        return null;
    }

    public static void sync(final CBSActivity context, final ModuleHandler handler) {
        instance._getFriendRequestList(context, new ModuleHandler<List<FriendRequest>>() {
            @Override
            public void onSuccess(List<FriendRequest> entity) {
                instance._getFriendList(context, new ModuleHandler<List<FriendRelation>>() {
                    @Override
                    public void onSuccess(List<FriendRelation> entity) {
                        instance._getBlackList(context, new ModuleHandler<List<FriendRelation>>() {
                            @Override
                            public void onSuccess(List<FriendRelation> entity) {
                                handler.onSuccess(null);
                            }

                            @Override
                            public void onFailure(int code, String message) {
                                handler.onFailure(code, message);
                            }

                            @Override
                            public void onException(Exception exception) {
                                handler.onException(exception);
                            }
                        });
                    }

                    @Override
                    public void onFailure(int code, String message) {
                        handler.onFailure(code, message);
                    }

                    @Override
                    public void onException(Exception exception) {
                        handler.onException(exception);
                    }
                });
            }

            @Override
            public void onFailure(int code, String message) {
                handler.onFailure(code, message);
            }

            @Override
            public void onException(Exception exception) {
                handler.onException(exception);
            }
        });
    }
}
